//登录注册 切换
function toggleBox(a, b, c) {
    function d() {
        f = setTimeout(function() {
            b.fadeOut();
            c && "function" === typeof c.leave && c.leave()
        }, 200)
    }
    function e() {
        clearTimeout(f);
        b.fadeIn();
        c && "function" === typeof c.enter && c.enter()
    }
    a = $(a);
    b = $(b);
    if (b.length && a.length) {
        var f = null;
        b.on("mouseenter", e);
        a.on("mouseleave", d);
        a.on("mouseenter", e);
        b.on("mouseleave", d)
    }
}

function trim(a) {
    return (a || "").replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " ")
}
function unescapeForHtml(a) {
    return (a || "").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&amp;/g, "&")
}
function objToUrlParam(a) {
    var b = [], c;
    for (c in a)
        b.push("&", c, "=", encodeURIComponent(a[c]));
    return b.join("")
}
function len(a) {
    return a ? a.replace(/<.*?>/g, "").replace(/[^\x00-\xff]/g, "rr").replace(/&nbsp;/g, " ").length : 0
}
function cutLength(a, b, c, d) {
    c = c || "...";
    d = d || 2;
    a = (a || "").replace(/<!.*?>/g, "");
    if (len(a) > b) {
        do
            a = a.substring(0, a.length - 1);
        while (a && len(a) + d > b);a.lastIndexOf("</") != a.lastIndexOf("<") && (a = a.substring(0, a.lastIndexOf("<")) + a.substring(a.lastIndexOf(">") + 1));
        return a + c
    }
    return a
}
function showFloat(a, b, c) {
    "right" == c ? a.css({
        top: b.offset().top + 30,
        left: b.offset().left
    }).fadeIn() : "middle" == c ? a.css({
        top: b.offset().top + 31,
        left: b.offset().left - (a.outerWidth(!0) / 2 - b.outerWidth() / 2)
    }).fadeIn() : a.css({
        top: b.offset().top + 31,
        left: b.offset().left - a.outerWidth(!0) / 2
    }).fadeIn()
}
function renderFloat(a, b, c, d, e) {
    b.html(juicer(c.html(), d));
    showFloat(b, a, e)
}
function fadeInAndOut(a, b, c, d, e, f) {
    renderFloat(a, b, c, d, f);
    var g = setTimeout(function() {
        b.fadeOut();
        $(".sc").removeClass("on").fadeOut();
        clearTimeout(g);
        g = null
    }, e)
}
function successBack(a) {
    return a && ("success" == a.code || "ok" == a.code || "exist" == a.code)
}

$(function(){
	$(".outbg").on('click',function(){
		$("#content-login").hide();
	})
	$("#loginBtn").on('click',function(){
		$("#content-login").show();
		$("#popup_login").removeClass("is-hidden");
		$("#popup_register").addClass("is-hidden");
	})
	$("#regBtn").on('click',function(){
		$("#content-login").show();
		$("#popup_register").removeClass("is-hidden");
		$("#popup_login").addClass("is-hidden");
	})
	$("#regin").on('click',function(){
		$("#popup_register").removeClass("is-hidden");
		$("#popup_login").addClass("is-hidden");
	})
	$("#logjoin").on('click',function(){
		$("#popup_login").removeClass("is-hidden");
		$("#popup_register").addClass("is-hidden");
    })

    var j = $(".yh");
    toggleBox(j, $(".login-pop"), {
        enter: function() {
            j.addClass("on")
        },
        leave: function() {
            j.removeClass("on")
        }
    });
    
    //点击注册 验证
    $('html').on('click','#popup_register .btn-sns',function(){
        var actionUrl = hrf+"index.php/doRegister";//ajax链接
        var phones = $("#popup_register input[name=phone]").val(),
            pwds = $("#popup_register input[name=password]").val(),
            nicknames = $("#popup_register input[name=name]").val();
        if (phones == "") {
            $("#popup_register input[name=name]").parent().after('<div class="retit">账号不能为空！</div>');
            setTimeout(function(){
                $(".retit").remove();
            },2000); 
            $("#popup_register input[name=phone]").focus();                
            return false;
        }            
        if (pwds == "") {
            $("#popup_register input[name=name]").parent().after('<div class="retit">密码不能为空！</div>');
            setTimeout(function(){
                $(".retit").remove();
            },2000); 
            $("#popup_register input[name=pwd]").focus();                
            return false;
        } 
        if (nicknames == "") {
            $("#popup_register input[name=name]").parent().after('<div class="retit">请取昵称！</div>');
            setTimeout(function(){
                $(".retit").remove();
            },2000); 
            $("#popup_register input[name=nickname]").focus();                
            return false;
        } 
        $.ajax({
            type: "POST",
            url: actionUrl,
            data: {"phone":phones,"password":pwds,"nickname":nicknames},
            success: function (data) {   
                console.log(data);   
                if (data.code == "200") {
                    console.log(data.msg);
                    $("#popup_register input[name=name]").parent().after('<div class="retit" style="border: 1px solid #00ae1c;color: #00ae1c; padding: 10px; margin: 10px 0;">'+data.msg+'</div>');
                    if(data.data){//如果注册并登陆成功，则保存登陆信息
                    	localStorage.setItem('dataArray', JSON.stringify(data.data));
                    }
                    setTimeout(function(){
                        $(".retit").remove();
                        location.replace(location.href);   
                    },2000); 
                    return true;                    
                }else if(data.code == 201){
                	$("#popup_register input[name=name]").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'注册失败，偶尔也有意外嘛'+'</div>');
                    setTimeout(function(){
                        $(".retit").remove();
                    },2000);        
                    return false;
                }else if(data.code == 202||data.code == 203||data.code == 204){
                	$("#popup_register input[name=name]").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'不告诉我可不行，请完善注册信息~'+'</div>');
                    setTimeout(function(){
                        $(".retit").remove();
                    },2000);        
                    return false;
                }else if(data.code == 205){
                	$("#popup_register input[name=name]").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'该账号已被别人抢先一步，换一个吧~'+'</div>');
                    setTimeout(function(){
                        $(".retit").remove();
                    },2000);        
                    return false;
                }else {
                    $("#popup_register input[name=name]").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'请重新注册'+'</div>');
                    setTimeout(function(){
                        $(".retit").remove();
                    },2000);        
                        //alert(data.data.msg);
                    return false;
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
               alert("网络错误");
               return false;
             }

        });
    }); 

    //查询是否有登陆
    var loginInfo = JSON.parse(localStorage.getItem('dataArray'));
    //当有登录 有useridssr 的时候就
    if(!loginInfo){
        //登录
        $('html').on('click','#popup_login .btn-sns',function(){
            var actionUrl = hrf+"index.php/login";//链接
            var phone = $("#popup_login .input-text").val(),
            pwd = $("#popup_login .login_pwd").val();
            console.log(phone+","+pwd);
            if (phone == "") {
                //alert("用户名不能为空！");
                $("#popup_login .login_pwd").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">用户名不能为空！</div>');
                setTimeout(function(){
                    $(".retit").remove();
                },2000); 
                $("#popup_login .input-text").focus();                
                return false;
            }            
            if (pwd == "") {
                //alert("密码不能为空！");
                $("#popup_login .login_pwd").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">密码不能为空！</div>');
                setTimeout(function(){
                    $(".retit").remove();
                },2000); 
                $("#popup_login .login_pwd").focus();                
                return false;
            }
             $.ajax({
                 type: "POST",
                 url: actionUrl,
                 data: {"phone":phone,"password":pwd},
                 success: function (data) {   
                     console.log(data);   
                     if (data.code == "200") {
                    	 console.log(data.msg); 
                         localStorage.setItem('dataArray', JSON.stringify(data.data));
                         location.replace(location.href);                 
                         return true;
                     }else if(data.code == "201"){
                    	 console.log('天体学原因，登录失败咯');
                         
                         $("#popup_login .login_pwd").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'天体学原因，登录失败咯'+'</div>');
                         
                         setTimeout(function(){
                             $(".retit").remove();
                         },2000);        
                             
                         return false;
                     }else if(data.code == "202"||data.code == "203"){
                    	 console.log('你不告诉我，我可无法登录哟');
                         
                         $("#popup_login .login_pwd").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'你不告诉我，我可无法登录哟'+'</div>');
                         
                         setTimeout(function(){
                             $(".retit").remove();
                         },2000);        
                             
                         return false;
                     }else if(data.code == "205"){
                         console.log('账号或密码错误,重新检查下吧');
          
                         $("#popup_login .login_pwd").parent().after('<div class="retit" style="border: 1px solid #d85757;color: #f10000; padding: 10px; margin: 10px 0;">'+'账号或密码错误,重新检查下吧'+'</div>');
                         
                         setTimeout(function(){
                             $(".retit").remove();
                         },2000);        
                             
                         return false;
                     }
                 },
                 error: function(XMLHttpRequest, textStatus, errorThrown) {
                     //debugger;
                    //alert("请确认您输入的用户名或密码输入是否正确！");
                    console.log("失败了");
                    return false;
                  }
             });
        });    
    }else{
        $('#login_no').hide();
        $('#login_yes').show();
        if(loginInfo.usertype != "1"){ //用户权限 删除会员中心入口
            $('#home').remove();
            $('.login-pop').css({'left':'-22px'});
        }
        var avata = '';
        if(loginInfo.avatar == ""){
            avata = hrf+'images/wechatcur.png';
        }else {
            avata = loginInfo.avatar;
        }
        $('.yh img').attr('src',avata);
        $('.yh span').html(loginInfo.nickname);
        //退出登录
        $("html").on('click',"#login_menu",function(){
             $.ajax({
                 type: "POST",
                 url: hrf+"index.php/logout",
                 data: {"phone":loginInfo.phone,"accesstoken":loginInfo.accesstoken},
                 dataType: 'json',
                 success: function (data) {   
                     console.log(data);         
                     if (data.code == "200"){
                        localStorage.removeItem('dataArray');
                        location.replace(hrf+'index.php/message');       
                        return true;
                     }else{   
                         alert(data.msg);
                         return false;
                     }
                 },
                 error: function(XMLHttpRequest, textStatus, errorThrown) {
                    //alert("退出失败请重试！");
                    //location.replace(location.href);    
                    return false;
                  }
             });
            //location.href = "index.html";
        });
    }

});